package com.scale.scales;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.scale.scales.adapter.IndexAdapter;
import com.scale.scales.bean.FoodBean;
import com.scale.scales.manager.DBManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recycler_base)
    public RecyclerView recycler_base;

    IndexAdapter indexAdapter;
    List<FoodBean> foodBeanList = new ArrayList<>();
    private DBManager dbManager;

    private SearchView searchView;

    int selected = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbManager = DBManager.getInstance(getApplication());
        ButterKnife.bind(this);

            GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
//            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            recycler_base.setLayoutManager(layoutManager);

        foodBeanList=dbManager.queryFoodList();

        indexAdapter = new IndexAdapter(MainActivity.this, foodBeanList);
        recycler_base.setAdapter(indexAdapter);
        indexAdapter.setOnItemClickListener(new IndexAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int positon) {

                if(selected==positon){
                    indexAdapter.notifyChange(foodBeanList,-1);
                }else {
                    indexAdapter.notifyChange(foodBeanList,positon);
                }



            }

            @Override
            public void onItemLongClick(View view, int positon) {

            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_child_activity, menu);
        MenuItem menuItem = menu.findItem(R.id.search);
        searchView = (SearchView) MenuItemCompat.getActionView(menuItem);

        //设置搜索的事件
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

//                transfer_create_refreshview.startRefresh();
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }


}
